﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

using System.ServiceModel.DomainServices.Client;

using LEEAAttendanceControl.Web;
using LEEAAttendanceControl.Web.Services;
using LEEAAttendanceControl;

namespace LEEAAttendanceControl.Views
{
    public partial class Login : Page
    {
        public Login()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            
        }

        private void loginButton_Click(object sender, RoutedEventArgs e)
        {
            errorSummary.Errors.Clear();

            if (nameBox.Text.Length == 0 && passwordBox.Password.Length == 0)
            {
                var error1 = new ValidationSummaryItem("Login is required.", "Login:", ValidationSummaryItemType.PropertyError
                    , null, null);

                var error2 = new ValidationSummaryItem("Password is required.", "Password:", ValidationSummaryItemType.PropertyError
                    , null, null);

                errorSummary.Errors.Add(error1);
                errorSummary.Errors.Add(error2);
            }
            else if (nameBox.Text.Length == 0 || passwordBox.Password.Length == 0)
            {
                if (nameBox.Text.Length == 0)
                {
                    var error = new ValidationSummaryItem("Login is required.", "Login:"
                        , ValidationSummaryItemType.PropertyError, null, null);

                    errorSummary.Errors.Add(error);
                }
                else if (passwordBox.Password.Length == 0)
                {
                    var error = new ValidationSummaryItem("Password is required.", "Password:"
                        , ValidationSummaryItemType.PropertyError, null, null);

                    errorSummary.Errors.Add(error);
                }
            }
            else
            {
                LEEAContext context = new LEEAContext();
                
                EntityQuery<LoginStatus> query = context.GetLoginStatusQuery(nameBox.Text, passwordBox.Password);

                LoadOperation<LoginStatus> load = context.Load(query, OnLoginCompleted, null);
            }
        }

        private void nameBox_GotFocus(object sender, RoutedEventArgs e)
        {
            nameBox.SelectAll();
        }

        private void passwordBox_GotFocus(object sender, RoutedEventArgs e)
        {
            passwordBox.SelectAll();
        }

        
        private void OnLoginCompleted(LoadOperation<LoginStatus> result)
        {
            if (result.HasError)
            {
                errorSummary.Errors.Clear();
                
                var error = new ValidationSummaryItem("Error, Please try again.", "Failed:"
                    , ValidationSummaryItemType.PropertyError, null, null);

                errorSummary.Errors.Add(error);

                result.MarkErrorAsHandled();
            }
            else
            {
                if (!result.Entities.FirstOrDefault().LoggedIn)
                {
                    errorSummary.Errors.Clear();

                    var error = new ValidationSummaryItem("Invalid Login/Password.", "Data:"
                        , ValidationSummaryItemType.PropertyError, null, null);

                    errorSummary.Errors.Add(error);
                }
                else
                {
                    App globalVar = (App)Application.Current;

                    globalVar.UserName = result.Entities.FirstOrDefault().Name;
                    globalVar.UserRole = result.Entities.FirstOrDefault().Role;
                    globalVar.DisplayName = result.Entities.FirstOrDefault().Display;

                    globalVar.CompanyID = result.Entities.FirstOrDefault().CompanyID;
                    globalVar.DepartmentID = result.Entities.FirstOrDefault().DepartmentID;
                    globalVar.EmployeeId = result.Entities.FirstOrDefault().EmployeeID;

                    if (result.Entities.FirstOrDefault().Role == "Simple User")
                    {
                        NavigationService.Navigate(new Uri("/Views/EmployeesView.xaml", UriKind.Relative));
                    }
                    else
                    {
                        NavigationService.Navigate(new Uri("/Views/MainView.xaml", UriKind.Relative));
                    }
                }
            }
        }
    }
}
